<?php
  include_once('colMapping.php');

  $xml = '';
  $gml = '';
  if (isset($_REQUEST['point']) && $_REQUEST['point'] != '') {
    $xml = simplexml_load_string(file_get_contents('http://geo.bluecityexchange.org/geoserver/crwa/ows?request=getfeature&service=wfs&version=1.0.0&typename=crwa:parcels&filter='.urlencode('<ogc:Filter xmlns:ogc="http://ogc.org" xmlns:gml="http://www.opengis.net/gml"><ogc:Contains><ogc:PropertyName>geom</ogc:PropertyName><gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml"><gml:coordinates>'.$_REQUEST['point'].'</gml:coordinates></gml:Point></ogc:Contains></ogc:Filter>')));
    $gml = 'coordinates';
  }
  else if (isset($_REQUEST['query'])) {
    $xml = simplexml_load_string(file_get_contents("http://geo.bluecityexchange.org/geoserver/crwa/ows?service=WFS&version=1.1.0&request=GetFeature&typeName=crwa:parcels&cql_filter=(parcelnum='".$_REQUEST['query']."')"));
    $gml = 'posList';
  }

  $node = array();
  foreach (array_keys($cols) as $c) {
    $pre = 'crwa';
    $field = $c;
    if ($c == 'posList') {
      $pre   = 'gml';
      $field = $gml;
    }
    $node[$c] = $xml->xpath("//$pre:$field");
    if ($c == 'posList' && $gml == 'coordinates') {
      // A 'point' request comes in w/ lon,lat, but a 'query' request doesn't.  So make them match.
      $a = array();
      foreach (explode(' ',(string)$node[$c][0]) as $pair) {
        $p = explode(',',$pair);
        $a[] = "$p[1] $p[0]"; 
      }
      $node[$c] = array(implode(' ',$a));
    }
  } 

  $a = array();
  foreach (array_keys($cols) as $c) {
    $a[$cols[$c]] = (string)$node[$c][0];
  }
  $data = array($a);

  echo json_encode(array('data' => $data));
?>
